package com.timepies.activityutils;

import static com.timepies.database.Service.getInstance;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.timepies.R;
import com.timepies.activity.DettaglioPieActivity;
import com.timepies.adapter.TimepieAdapter;
import com.timepies.adapter.TimepieAdapterDatabase;
import com.timepies.domain.Festivita.YearOutOfRangeException;
import com.timepies.domain.MenuPies;
import com.timepies.domain.Timepie;

public class ElencoList implements OnItemClickListener {

	private Activity activity;
	private BaseAdapter adapter;
	private ListView list;

	public ListView getList() {
		return list;
	}

	public BaseAdapter getAdapter() {
		return adapter;
	}

	public ElencoList(Activity activity) {

		this.activity = activity;
		this.list = (ListView) activity.findViewById(R.id.pie_main_list);
		this.list.setOnItemClickListener(this);

		// carica l'ultimo elenco visualizzato
		load(getInstance(activity).getMenuPies().getPosizione());
	}

	public void load(int itemPosition) {

		try {
			Log.d(this.toString(), "load p:" + itemPosition);
			MenuPies menu = MenuPies.valueOf(itemPosition);
			String nuovoTitolo = "";// titolo
			String[] c; // contatore statistiche

			switch (menu) {

			case FESTIVITA:
				c = getInstance(activity).countFesteAndPonti().split(",");
				nuovoTitolo = String.format(menu.getTitolo(), c[0], c[1], c[1]);
				this.adapter = new TimepieAdapter(activity, R.id.listView,
						getInstance(activity).calcolaProssimeFeste());
				this.list.setAdapter(adapter);
				break;

			case PONTI:
				c = getInstance(activity).countFesteAndPonti().split(",");
				int sum = Integer.parseInt(c[1]) + Integer.parseInt(c[3]);
				nuovoTitolo = String.format(menu.getTitolo(), c[0], c[1], c[1],
						c[2], sum, c[3]);
				this.adapter = new TimepieAdapter(activity, R.id.listView,
						getInstance(activity).calcolaProssimiPonti());
				this.list.setAdapter(adapter);
				break;

			case TIMEPIES:
				nuovoTitolo = menu.getTitolo();
				Cursor cursor = getInstance(activity).cursor();
				this.adapter = new TimepieAdapterDatabase(activity, cursor, 0);
				this.list.setAdapter(adapter);

				// mostra o meno le istruzioni
				boolean vuota = list.getCount() == 0;
				TextView istruzioni = (TextView) activity
						.findViewById(R.id.istruzioni);
				istruzioni.setVisibility((vuota) ? View.VISIBLE : View.GONE);

				break;
			case SHAREDPIES:
				nuovoTitolo = menu.getTitolo();
				this.adapter = new TimepieAdapter(activity, R.id.listView,
						getInstance(activity).getList());
				this.list.setAdapter(adapter);
				break;

			}

			// default action
			TextView titolo = (TextView) activity
					.findViewById(R.id.mainlist_title);
			titolo.setText(nuovoTitolo);
			titolo.setBackgroundColor(Color.parseColor(menu.getColore()));
			Log.d(this.toString(), " Nuovo Titolo :" + nuovoTitolo);
			Log.d(this.toString(), " Menu Colore :" + menu.getColore());

			// salva l'ultimo elenco visualizzato
			getInstance(activity).setMenuPies(menu);
			activity.getActionBar().setTitle(menu.getDescrizione());
			Log.d(this.toString(), " caricato elenco :" + menu.getDescrizione());

			activity.invalidateOptionsMenu();

		} catch (YearOutOfRangeException e) {
			Log.e(this.toString(), "error : " + e.getMessage());
			Log.e(this.toString(), "error : " + e.getCause());
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

		String data = ((TextView) v.findViewById(R.id.txt_date)).getText()
				.toString();

		String testo = ((TextView) v.findViewById(R.id.txt_testo)).getText()
				.toString();

		String oggetto = ((TextView) v.findViewById(R.id.txt_oggetto))
				.getText().toString();

		Intent i = new Intent(activity, DettaglioPieActivity.class);
		i.putExtra("pieObj", new Timepie(id, data, oggetto, testo));
		activity.startActivity(i);
		activity.finish();

		Log.d(this.toString(), "p:" + position + " id:" + id + " SENDED!");

	}
}
